package com.naiterui.ehp.bs.emr.controller.inner;

import com.naiterui.ehp.bp.bo.emr.DrRecordIdBO;
import com.naiterui.ehp.bp.bo.emr.InquireCaseBO;
import com.naiterui.ehp.bp.bo.emr.MedicalRecordBO;
import com.naiterui.ehp.bp.bo.emr.MedicalRecordDoctorBO;
import com.naiterui.ehp.bp.bo.emr.MedicalRecordIdBO;
import com.naiterui.ehp.bp.bo.emr.MedicalRecordScreenBO;
import com.naiterui.ehp.bp.bo.emr.RecordRecomIdBO;
import com.naiterui.ehp.bp.bo.emr.params.DrCaseParams;
import com.naiterui.ehp.bp.bo.emr.params.RevisitNoticeParamBO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.bp.utils.lang.StringUtil;
import com.naiterui.ehp.bs.emr.exception.ExceptionCodes;
import com.naiterui.ehp.bs.emr.service.IDrCaseService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

@Validated
@Api(tags = SwaggerTag.INNER_API + SwaggerTag.MEDICAL_RECORD)
@RestController
@RequestMapping("emr/inner/medicalRecord")
public class EmrInnerMedicalRecordController {
    public static final Logger LOGGER = LoggerFactory.getLogger(EmrInnerMedicalRecordController.class);

    @Autowired
    private IDrCaseService drCaseService;

    /**
     * 查询患者最近本人病历id（医生填写病历或患者发送病历）
     * InnerMedicalRecordController.getMedicalRecordId()
     *
     * @param doctorId
     * @param patientId
     *
     * @return
     *
     * @throws BusinessException
     * @Author lichaopi
     * @Date 2018年5月31日
     * @since 1.0.0
     */
    @RequestMapping("getId")
    public ResponseVO<MedicalRecordIdBO> getMedicalRecordId(@NotNull(message = "医生id不能为null") Long doctorId,
                                                            @NotNull(message = "患者id不能为null") Long patientId) {
        LOGGER.info("查询患者最近病历id,doctorId:{},patientId:{}", doctorId, patientId);
        MedicalRecordIdBO bo = drCaseService.getMedicalRecordId(doctorId, patientId);
        return PublicService.returnResponseVO(bo);
    }

    /**
     * 添加处方、诊疗记录
     * InnerMedicalRecordController.addMedicalRecord()
     *
     * @return
     *
     * @throws BusinessException
     * @Author lichaopi
     * @Date 2018年5月31日
     * @since 1.0.0
     */
    @RequestMapping("addMedicalRecord")
    public ResponseVO<Void> addMedicalRecord(@NotNull(message = "添加诊记录或处方不能为null") @RequestBody MedicalRecordBO bo) {
        drCaseService.addMedicalRecordByBO(bo);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * 病历页筛选条件
     * InnerMedicalRecordController.getScreenList()
     */
    @RequestMapping("screenList")
    public ResponseVO<List<MedicalRecordScreenBO>> getScreenList(@NotNull(message = "医生id不能为null") Long doctorId,
                                                                 @NotNull(message = "患者id不能为null") Long patientId) {
        LOGGER.info("病历页筛选条件,doctorId:{},patientId:{}", doctorId, patientId);
        return PublicService.returnResponseVO(drCaseService.getScreenList(doctorId, patientId));
    }

    /**
     * 提醒患者和医生复诊
     * InnerMedicalRecordController.notifyRevisit()
     *
     * @return
     *
     * @Author lichaopi
     * @Date 2018年6月1日
     * @since 1.0.0
     */
    @RequestMapping("notifyRevisit")
    public ResponseVO<Void> notifyRevisit(Long dId) {
        LOGGER.info("患者和医生复诊提醒开始,dId:{}", dId);
        drCaseService.notifyRevisit(dId);
        LOGGER.info("患者和医生复诊提醒结束");
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * 保存病历
     * InnerMedicalRecordController.saveDrCase()
     *
     * @param params
     *
     * @return
     *
     * @throws BusinessException
     * @Author lichaopi
     * @Date 2018年6月28日
     * @since 1.0.0
     */
    @RequestMapping("saveDrCase")
    public ResponseVO<DrRecordIdBO> saveDrCase(@RequestBody @Valid DrCaseParams params) throws BusinessException {
        LOGGER.info("处方保存病历请求参数,params:{}", params.toString());
        DrRecordIdBO bo = drCaseService.saveDrRecordTemporary(params);
        return PublicService.returnResponseVO(bo);
    }

    /**
     * 查询医生填写病历Bo,判断推荐是否包含病历
     * InnerMedicalRecordController.getDrCaseByRecomId()
     *
     * @param recommendId
     *
     * @return
     *
     * @Author lichaopi
     * @Date 2018年6月28日
     * @since 1.0.0
     */
    @RequestMapping("getDrCaseByRecomId")
    public ResponseVO<MedicalRecordDoctorBO> getDrCaseByRecomId(@NotNull(message = "推荐id不能为null") Long recommendId) {
        LOGGER.info("查询医生填写病历Bo,传参recommendId:{}", recommendId);
        MedicalRecordDoctorBO medicalRecordDoctorBO = drCaseService.getDrRecordBOMessage(recommendId);
        return PublicService.returnResponseVO(medicalRecordDoctorBO);
    }

    /**
     * 更新病历状态
     * InnerMedicalRecordController.updateDrCase()
     *
     * @param recordId
     *
     * @return
     *
     * @throws BusinessException
     * @Author lichaopi
     * @Date 2018年6月28日
     * @since 1.0.0
     */
    @RequestMapping("updateDrCase")
    public ResponseVO<Void> updateDrCase(@NotEmpty(message = "病历ID不能为null") String recordId) throws BusinessException {
        LOGGER.info("更新医生填写病历，传参：recordId:{}", recordId);
        drCaseService.updateDrCase(recordId);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * 校验指定时间内是否存在患者医嘱提醒
     * InnerMedicalRecordController.checkRevisitInfo()
     *
     * @param revisitNoticeParamBO
     *
     * @Date 2018年6月28日
     * @since 2.19
     */
    @RequestMapping("checkRevisitInfo")
    public ResponseVO<Boolean> checkRevisitInfo(@Valid RevisitNoticeParamBO revisitNoticeParamBO) {
        LOGGER.info("校验指定时间内是否存在患者医嘱提醒 请求参数：revisitNoticeParamBO = {}", revisitNoticeParamBO);
        boolean existFlag = drCaseService.getRevisitInfo(revisitNoticeParamBO);
        return PublicService.returnResponseVO(existFlag);
    }

    /**
     * 获取主诉既往史必填标识
     * InnerMedicalRecordController.getRequiredFlag()
     *
     * @return
     *
     * @throws BusinessException
     * @Author lichaopi
     * @Date 2018年6月29日
     * @since 1.0.0
     */
    @RequestMapping("requiredFlag")
    public ResponseVO<Integer> getRequiredFlag() {
        Integer requiredFlag = Integer.parseInt(ConfigUtil.getString("main.complaint.flag"));
        return PublicService.returnResponseVO(requiredFlag);
    }

    /**
     * 根据推荐id列表获取病历id列表
     * InnerMedicalRecordController.getRecordRecomIdList()
     *
     * @param recomIds
     *
     * @return
     *
     * @throws BusinessException
     * @Author lichaopi
     * @Date 2018年6月30日
     * @since 1.0.0
     */
    @RequestMapping("getRecordRecomIdList")
    public ResponseVO<List<RecordRecomIdBO>> getRecordRecomIdList(@NotEmpty(message = "根据推荐id列表获取病历id列表参数不能为null") String recomIds) {
        LOGGER.info("根据推荐id列表获取病历id列表,recomIds:{}", recomIds);
        List<Long> recomIdList = StringUtil.convertLongStr2List(recomIds, ",");
        List<RecordRecomIdBO> list = drCaseService.getRecordRecomIdBO(recomIdList);
        return PublicService.returnResponseVO(list);
    }

    /**
     * 获取患者首聊：病情描述、病历描述文案
     *
     * @param doctorId
     * @param patientId
     * @param type
     *
     * @return
     */
    @RequestMapping("getDiseaseContent")
    public ResponseVO<String> getDiseaseContent(
            @NotNull(message = "医生ID不可为空") Long doctorId,
            @NotNull(message = "患者ID不可为空") Long patientId,
            @NotNull(message = "就诊人ID不可为空") Long inquirerId,
            @NotNull(message = "患者病历类型不可为空") Integer type,
            @NotNull(message = "记录ID不可为空") String recordId) {
        LOGGER.info("获取患者首聊：病情描述、病历描述文案 , doctorId={},patientId={},type={},recordId={}", doctorId, patientId, type, recordId);
        String result = drCaseService.getDiseaseContent(doctorId, patientId, inquirerId, type, recordId);
        return PublicService.returnResponseVO(result);
    }

    /**
     * 获取医生是否给指定患者填写过病历(0: 无，1：有)
     */
    @PostMapping("exist")
    public ResponseVO<Integer> existMedicalRecord(
            @NotNull(message = "医生ID不可为空") Long doctorId,
            @NotNull(message = "患者ID不可为空") Long patientId) {
        LOGGER.info("获取医生是否给指定患者填写过病历,doctorId={},patientId={}", doctorId, patientId);
        boolean exist = drCaseService.getCaseExist(patientId, doctorId);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, exist ? 1 : 0);
    }

    /**
     * 获取就诊人病例信息：可根据需要进行扩展
     *
     * @param recordId
     * @return
     */
    @RequestMapping("inquirer/case")
    public ResponseVO<InquireCaseBO> inquirerCase(
            @NotNull(message = "记录ID不可为空") String recordId) throws BusinessException{
        LOGGER.info("获取患者病例诊断 , recordId={}" , recordId);
        InquireCaseBO result = drCaseService.inquirerCaseInfo(recordId);
        return PublicService.returnResponseVO(result);
    }

}
